package one.bugu.android.demon.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.juefeng.android.framework.LKUtil;
import com.juefeng.android.framework.view.annotation.LKContentView;
import com.juefeng.android.framework.view.annotation.LKInjectView;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.ShortMessageInfoBean;
import one.bugu.android.demon.common.BaseHttpAsycResponceHandler;
import one.bugu.android.demon.common.MyBaseActivity;
import one.bugu.android.demon.constant.Constant;
import one.bugu.android.demon.constant.HttpConstant;
import one.bugu.android.demon.ui.adapter.ShortMessageInfoAdapter;
import one.bugu.android.demon.ui.widget.BaseTopBarView;
import one.bugu.android.demon.util.IntentUtils;
import one.bugu.android.demon.util.PreferencesUtil;
import one.bugu.android.demon.util.StatusBarUtils;

/**
 * 短信详情页面
 */
@LKContentView(R.layout.activity_shortmessageinfo)
public class ShortMessageActivity extends MyBaseActivity {

    /**
     * 短信信息列表
     */
    @LKInjectView(R.id.shortMessage_listview)
    private ListView listView;

    @LKInjectView(R.id.btbv_sm_top_bar)
    private BaseTopBarView mSmTopBar;

    /**
     * 短信数量
     */
    @LKInjectView(R.id.shortMessage_num)
    private TextView shortMessageNum;

    /**
     * 去兑换按钮
     */
    @LKInjectView(R.id.tv_to_exchange)
    private TextView toExchange;

    private ShortMessageInfoAdapter shortMessageInfoAdapter;

    String token;


    @Override
    public void initView() {
        super.initView();

        StatusBarUtils.getInstance().setStatusBar(this, mSmTopBar);

        token = PreferencesUtil.getInstance().getString(ShortMessageActivity.this, Constant.TOKEN);

        shortMessageInfoAdapter = new ShortMessageInfoAdapter(this);
        listView.setAdapter(shortMessageInfoAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        String msgNum = PreferencesUtil.getInstance().getString(this, Constant.SHORTMESSAGENUM, "0");
        shortMessageNum.setText(msgNum);
    }

    /**
     * 获取短信详细信息
     */
    @Override
    protected void initData() {
        super.initData();

        getData();
    }

    /**
     * 获取数据
     */
    private void getData() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("limit", "100");
        map.put("offset", "1");
        map.put("optClass", "10");
        map.put("order", "desc");
        map.put("sort", "optTime");
        LKUtil.getHttpManager().postBody(HttpConstant.BGT_URL, map, handler);
    }

    /**
     * 获取短信信息详情网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<List<ShortMessageInfoBean>> handler = new BaseHttpAsycResponceHandler<List<ShortMessageInfoBean>>(false) {
        @Override
        public void onSuccess(List<ShortMessageInfoBean> shortMessageInfoBeans) {
            super.onSuccess(shortMessageInfoBeans);

            shortMessageInfoAdapter.updateData(shortMessageInfoBeans);
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);
        }

        @Override
        public void onError() {
            super.onError();
        }
    };

    @Override
    protected void initEvent() {
        super.initEvent();

        mSmTopBar.setEventInterface(new BaseTopBarView.EventInterface() {
            @Override
            public void leftOnClick() {
                finish();
            }

            @Override
            public void leftCloseClick() {

            }

            @Override
            public void rightOnClick() {

            }
        });

        //去兑换按钮
        toExchange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentUtils.startAtyForResult(ShortMessageActivity.this, ExchangeServiceActivity.class, 1002);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1002 && resultCode == RESULT_OK) {
            getData();
        }
    }
}
